/**
 * this class is the lightweight wrapper of MIDI API. the system has two synthesizers, so we need a 
 * the main purpose of facade class is introducing pagebus component and making the web UI loose coupled
 * depends on var constants = new com.gdgsuzhou.webmusic.Constants(); some message key used
 * @author maoanapex88@163.com Mao Anping
 */
jweb.gdgsuzhou.webmusic.MidiHub = function(params){
	function cleanArg(key, defautValue) {
      return (params[key] != null && params[key] != "") ? params[key] : defautValue;
	}
	this.audioContext=null;
	this.midiAccess=null;
	this.start();
}
jweb.gdgsuzhou.webmusic.MidiHub.prototype = {
	onMessage:function(subj, msg, data) {
		if(subj == constants.contextInited){
			this.audioContext = msg.audioContext;//refer the obj
			window.PageBus.publish(constants.topicLog, {level:'DEBUG', content:'audioContext for midi hub bind'});
			if(navigator.requestMIDIAccess){
				navigator.requestMIDIAccess({sysex: false}).then(this.onMIDIAccessSuccessCallback, this.onMIDIAccessFailCallback);
			}
			else {
				window.PageBus.publish(constants.topicLog, {level:'FATAL', content:'No Audio support in your browser.'});
			}
		}
	},
	start:function() {
		if(this.subscriberId==null) this.subscriberId='**';
		this.subscriberId = window.PageBus.subscribe('**', this, this.onMessage, null);
		//noew we do not need wavetale base on sf2synth, publish audiocontext here
		window.AudioContext=window.AudioContext||window.webkitAudioContext;
		var audioContext = new AudioContext();
		window.PageBus.publish('contextInited', {audioContext:audioContext});
	},
	onMIDIAccessSuccessCallback:function(midiAccess){
		var $this = this;
		this.midiAccess = midiAccess;
		window.PageBus.publish(constants.midiAccessed, {midiAccess:this.midiAccess});
		window.PageBus.publish(constants.topicLog, {level:'DEBUG', content:'midiAccess hub connected'});
		midiAccess.inputs.forEach(function(port){
			window.PageBus.publish(constants.midiDevice, {ioType:'input', data:port});
		});
		midiAccess.outputs.forEach(function(port){
			window.PageBus.publish(constants.midiDevice, {ioType:'output', data:port});
		});
		var inputs=midiAccess.inputs.values();
		for ( var input = inputs.next(); input && !input.done; input = inputs.next()) {
			input.value.onmidimessage = function(e){
				window.PageBus.publish(constants.midiMessage, {event:e, midiAccess:$this.midiAccess, audioContext:$this.audioContext});
			};
		}
		midiAccess.onstatechange=function(){
			var $this = this;
			midiAccess.inputs.forEach(function(port){
				window.PageBus.publish(constants.midiDevice, {ioType:'input', data:port});
			});
			midiAccess.outputs.forEach(function(port){
				window.PageBus.publish(constants.midiDevice, {ioType:'output', data:port});
			});
			var inputs=midiAccess.inputs.values();
			for ( var input = inputs.next(); input && !input.done; input = inputs.next()) {
				input.value.onmidimessage = function(e){
					window.PageBus.publish(constants.midiMessage, {event:e, midiAccess:$this.midiAccess, audioContext:$this.audioContext});
				};
			}
		};
	},
	onMIDIAccessFailCallback:function(data){
		window.PageBus.publish(constants.topicLog, {level:'FATAL', content:'No Audio support in your browser.' + data});
	}
}
